﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Code;
using System.Data;

namespace E2_WEBSITE.Code.CPU
{
    public class CPUDataAccess
    {
        private DataProvider m_DataProvider = DataProvider.GlobalDataProvider;
        public List<string> GetListSocketByManufactureName(string sName)
        {
            List<string> lstSocket = new List<string>();
            string sSql = "select distinct socket from CPU,SAN_PHAM,NHA_SAN_XUAT where SAN_PHAM.idNSX = NHA_SAN_XUAT.id AND NHA_SAN_XUAT.TenNSX = '" + sName
                + "' AND SAN_PHAM.id=CPU.id";

            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            if(dt != null)
            {
                int iCount = dt.Rows.Count;
                for(int i = 0;i < iCount ;i++)
                {
                    lstSocket.Add(dt.Rows[i][0].ToString());
                }
            }
            return lstSocket;

        }

        public List<string> GetListBusByManufactureNameAndSocket(string sManufactureName,string sSocket)
        {
            List<string> lstBus = new List<string>();
            string sSql = "select distinct bus from CPU,SAN_PHAM,NHA_SAN_XUAT where SAN_PHAM.idNSX = NHA_SAN_XUAT.id AND NHA_SAN_XUAT.TenNSX = '" + sManufactureName
            + "' AND Socket='" + sSocket + "' AND SAN_PHAM.id=CPU.id";

            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            if (dt != null)
            {
                int iCount = dt.Rows.Count;
                for (int i = 0; i < iCount; i++)
                {
                    lstBus.Add(dt.Rows[i][0].ToString());
                }
            }
            return lstBus;
        }

        public List<string> GetListSpeedByManuAndSocketAndBus(string sManufacture, string sSocket, string sBus)
        {
            List<string> lstSpeed = new List<string>();
            string sSql = "select distinct speed from CPU,SAN_PHAM,NHA_SAN_XUAT where SAN_PHAM.idNSX = NHA_SAN_XUAT.id AND NHA_SAN_XUAT.TenNSX = '" + sManufacture
            + "' AND Socket='" + sSocket + "' AND BUS ='" + sBus + "' AND SAN_PHAM.id=CPU.id";

            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            if (dt != null)
            {
                int iCount = dt.Rows.Count;
                for (int i = 0; i < iCount; i++)
                {
                    lstSpeed.Add(dt.Rows[i][0].ToString());
                }
            }
            return lstSpeed;
        }

        public List<string> GetListProductNameByManuAndSocketAndBusandSpeed(string sManufacture,string sSocket,string sBus,string sSpeed)
        {
            List<string> lstProName = new List<string>();
            string sSql = "select distinct TenSanPham from CPU,SAN_PHAM,NHA_SAN_XUAT where SAN_PHAM.idNSX = NHA_SAN_XUAT.id AND NHA_SAN_XUAT.TenNSX = '" + sManufacture
            + "' AND Socket='" + sSocket + "' AND BUS ='" + sBus + "' AND SPEED ='" + sSpeed +"' AND SAN_PHAM.id=CPU.id";
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            if (dt != null)
            {
                int iCount = dt.Rows.Count;
                for (int i = 0; i < iCount; i++)
                {
                    lstProName.Add(dt.Rows[i][0].ToString());
                }
            }
            return lstProName;
        }

        public int GetCPUIDByProductNameAndManuAndSocketAndBusAndSpeed(string sManufacture,string sSocket,string sBus,string sSpeed,string sName)
        {
            int iId = -1;
            string sSql = "select distinct SAN_PHAM.id from CPU,SAN_PHAM,NHA_SAN_XUAT where SAN_PHAM.idNSX = NHA_SAN_XUAT.id AND NHA_SAN_XUAT.TenNSX = '" + sManufacture
            + "' AND Socket='" + sSocket + "' AND BUS ='" + sBus + "' AND SPEED ='" + sSpeed + "'AND TENSANPHAM ='" + sName + "' AND SAN_PHAM.id=CPU.id";
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            if (dt != null)
            {
               iId = int.Parse(dt.Rows[0][0].ToString());
                
            }
            return iId;
        }
    }
}
